package com.winning.devops.batch.decider.config;

import com.winning.devops.batch.decider.FirstJobExecutionDecider;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chensj
 * @date 2020-02-28 16:50
 */
@Configuration
@EnableBatchProcessing
public class DeciderDemoJobConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    /**
     * 定义决策器
     *
     * @return
     */
    @Bean
    public JobExecutionDecider firstJobExecutionDecider() {
        return new FirstJobExecutionDecider();
    }

    @Bean
    public Job deciderDemoJob() {
        return jobBuilderFactory.get("deciderDemoJob")
                .start(deciderDemoStep1())
                // 使用决策器
                .next(firstJobExecutionDecider())
                // 决策器结果是even执行 step2
                .from(firstJobExecutionDecider()).on("even").to(deciderDemoStep2())
                // 决策器结果是odd执行 step3
                .from(firstJobExecutionDecider()).on("odd").to(deciderDemoStep3())
                // 执行 step3 不管结果如何 进入决策器 再次到判断结果
                .from(deciderDemoStep3()).on("*").to(firstJobExecutionDecider())
                .end().build();


    }


    @Bean
    public Step deciderDemoStep1() {
        return stepBuilderFactory
                .get("deciderDemoStep1")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("deciderDemoStep1");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Bean
    public Step deciderDemoStep2() {
        return stepBuilderFactory
                .get("deciderDemoStep2")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("deciderDemoStep2");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Bean
    public Step deciderDemoStep3() {
        return stepBuilderFactory
                .get("deciderDemoStep3")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("deciderDemoStep3");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }
}
